////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2012 www.iflytek.com. All Rights Reserved.
//  This software for customer relationship management system, developed by Noo team.
//  Software code and design for the team, copy rights reserved.
////////////////////////////////////////////////////////////////////////////////

package com.iflytek.ci.shiro;

import com.google.common.base.Objects;
import com.iflytek.ci.cums.service.impl.EncryptionPassword;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * <p>
 * 身份验证.
 * </p>
 *
 * @author poplar.yfyang
 * @version 1.0 2012-10-25 9:54 PM
 * @since JDK 1.5
 */
public class MyCredentialsMather extends SimpleCredentialsMatcher {
	/**
	 * This implementation acquires the {@code token}'s credentials
	 * (via {@link #getCredentials(org.apache.shiro.authc.AuthenticationToken) getCredentials(token)})
	 * and then the {@code account}'s credentials
	 * (via {@link #getCredentials(org.apache.shiro.authc.AuthenticationInfo) getCredentials(account)}) and then passes both of
	 * them to the {@link #equals(Object, Object) equals(tokenCredentials, accountCredentials)} method for equality
	 * comparison.
	 *
	 * @param token the {@code AuthenticationToken} submitted during the authentication attempt.
	 * @param info  the {@code AuthenticationInfo} stored in the system matching the token principal.
	 * @return {@code true} if the provided token credentials are equal to the stored account credentials,
	 *         {@code false} otherwise
	 */
	@Override
	public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
		Object tokenCredential = EncryptionPassword.encryption(toString(token.getCredentials()));
		Object userPassword = this.getCredentials(info);
		return Objects.equal(tokenCredential, userPassword);
	}
}
